<?php
include_once "conn.php";
$token=$_POST['token']; //获取token，内含个人信息
$id=$_POST['id']; //获取商品id
$num=$_POST['num'];//获取商品数量
$car_id=$_POST['car_id']; //获取购物车id
$isCarFrom=$_POST['isCarFrom']; //是否从购物车而来
// 订单
$sql=$db->query("INSERT INTO order_user (user_id) VALUES 
                ((SELECT user_id FROM user WHERE user_token = '$token')) ");
$insert_id=$db->insert_id; //获取最近一次自动增长的id值
foreach ($id as $value) {
    $db->query("INSERT INTO order_goods (goods_id,order_user_id) VALUES
                ('$value','$insert_id')");
    foreach ($num as $v) {
        $db->query("UPDATE order_goods SET order_num = '$v' WHERE order_user_id = '$insert_id' AND goods_id = '$value'");
        //由于后面跳出，删除数组第一个元素，下次为第二个元素
        array_shift($num); 
        break;
    }
}
if($isCarFrom === 'true'){ //从购物车而来的v订单需要删除购物车相关商品
    foreach ($car_id as $value) {
        $db -> query("DELETE FROM car WHERE car_id = '$value' ");
    }
}
$db->close();
?>